package com.yiding.sys.common.model;

import com.yiding.sys.common.enums.ResultCodeEnum;

/**
 * @author War horse imwarhorse@aliyun.com
 * Date: 2020/8/7
 * Description: No Description
 */
public class Result<T> {

	private int code;
	private String msg;
	private T data;

	// 使用链式编程的方式优化使用体验，也可以使用传统的getter/setter
	public Result<T> setCode(int code) {
		this.code = code;
		return this;
	}

	public int getCode() {
		return code;
	}

	public Result<T> setMsg(String msg) {
		this.msg = msg;
		return this;
	}

	public String getMsg() {
		return msg;
	}

	public Result<T> setData(T data) {
		this.data = data;
		return this;
	}

	public T getData() {
		return data;
	}

	private Result<T> setCodeAndMsg(ResultCodeEnum resultCodeEnum) {
		this.code = resultCodeEnum.getCode();
		this.msg = resultCodeEnum.getName();
		return this;
	}

	public Result<T> geneSuccess(T data) {
		return new Result<T>().setCode(ResultCodeEnum.SUCCESS.getCode()).setMsg(ResultCodeEnum.SUCCESS.getName()).setData(data);
	}

	public Result<T> geneFailed(T data) {
		return new Result<T>().setCodeAndMsg(ResultCodeEnum.FAILED).setData(data);
	}

	public Result<T> geneSuccess() {
		return new Result<T>().setCodeAndMsg(ResultCodeEnum.SUCCESS);
	}

	public Result<T> geneFailed() {
		return new Result<T>().setCodeAndMsg(ResultCodeEnum.FAILED);
	}

	/**
	 * 不建议使用改方式，建议将所有可能出现的错误结果写入ResultCodeEnum
	 * @param msg 错误消息
	 * @return 统一结果封装类
	 */
	@Deprecated
	public Result<T> geneSuccess(String msg) {
		return new Result<T>().setMsg(msg).setCode(ResultCodeEnum.SUCCESS.getCode());
	}

	/**
	 * 不建议使用改方式，建议将所有可能出现的错误结果写入ResultCodeEnum
	 * @param msg 错误消息
	 * @return 统一结果封装类
	 */
	@Deprecated
	public Result<T> geneFailed(String msg) {
		return new Result<T>().setMsg(msg).setCode(ResultCodeEnum.FAILED.getCode());
	}

	public Result<T> geneFailed(ResultCodeEnum resultCodeEnum) {
		this.code = resultCodeEnum.getCode();
		this.msg = resultCodeEnum.getName();
		return this;
	}

	public Result<T> geneSuccess(ResultCodeEnum resultCodeEnum) {
		this.code = resultCodeEnum.getCode();
		this.msg = resultCodeEnum.getName();
		return this;
	}
}
